package com.framework.modules.job.task;

import com.framework.entity.log.LogAccount;
import com.framework.service.TransactionInfoService;
import com.framework.utils.MD5Util;
import com.framework.utils.httpClient.HttpLogin;
import com.framework.utils.jiufu.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.xml.bind.Element;
import java.util.List;

/**
 * 功能描述: 交易定时器
 *
 * @author ck
 * @date 2018/11/22 11:25
 */
@Component("transactionTask")
public class TransactionTask {
	private Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private TransactionInfoService transactionInfoService;

	protected void addTransactionKaYou() {
		logger.info("卡友交易定时器开始=======================addTransactionKaYou");
		//查询所有渠道下面的卡友账户信息
		List<LogAccount> list = transactionInfoService.queryTerAccountList(1);
		for (int i = 0; i < list.size(); i++) {
			//登录账号
			String userName = list.get(i).getUserName();
			//密码
			String password = list.get(i).getPassword();
			//账号id
			Long accountId = list.get(i).getAccountId();
			//说明
			String expInfo = list.get(i).getExpInfo();
			//二维码地址
			String imgCodeUrl = list.get(i).getImgCodeUrl();
			//登录地址
			String loginUrl = list.get(i).getLoginUrl();

			String sessionId=null;
			try {
				sessionId= HttpLogin.kaYouLogin(0,userName,password,loginUrl,imgCodeUrl,"你好，卡友合伙人");
			} catch (Exception e){
				transactionInfoService.insertFailTerLog(expInfo+"-"+e.getMessage(),accountId);
				break;
			}
				if(StringUtils.isBlank(sessionId)){
					//添加失败日志
					transactionInfoService.insertFailTerLog(expInfo+"-识别验证码失败",accountId);
				}else{
					transactionInfoService.addTransactionKaYou(sessionId,accountId,expInfo);
				}



		}
		logger.info("卡友交易定时器结束=======================addTransactionKaYou");
	}

	protected void addTransactionKaiDianBao() {
		logger.info("开店宝定时器开始=======================addTransactionKaYou");
		//查询所有渠道下面的卡友账户信息
		List<LogAccount> list = transactionInfoService.queryTerAccountList(1);
		for (int i = 0; i < list.size(); i++) {
			//登录账号
			String userName = list.get(i).getUserName();
			//密码
			String password = list.get(i).getPassword();
			//账号id
			Long accountId = list.get(i).getAccountId();
			//说明
			String expInfo = list.get(i).getExpInfo();
			//二维码地址
			String imgCodeUrl = list.get(i).getImgCodeUrl();
			//登录地址
			String loginUrl = list.get(i).getLoginUrl();
			String sessionId = HttpLogin.kaYouLogin(0,userName,password,loginUrl,imgCodeUrl,"你好，信创");
			if(StringUtils.isBlank(sessionId)){
				//添加失败日志
				transactionInfoService.insertFailTerLog(expInfo+"-识别验证码失败",accountId);
			}else{
				transactionInfoService.addTransactionKaiDianBao(sessionId,accountId,expInfo);
			}
		}
		logger.info("开店宝定时器结束=======================addTransactionKaYou");
	}

	protected void addTransactionLeShua() {
		logger.info("乐刷交易定时器开始=======================addTransactionLeShua");
		//查询所有渠道下面的乐刷账户信息
		List<LogAccount> list = transactionInfoService.queryTerAccountList(5);
		for (int i = 0; i < list.size(); i++) {
			String userName = list.get(i).getUserName(); //登录账号
			String password = list.get(i).getPassword(); //密码
			Long accountId = list.get(i).getAccountId(); //账号id
			String expInfo = list.get(i).getExpInfo();   //说明
			String imgCodeUrl = list.get(i).getImgCodeUrl();//二维码地址
			String loginUrl = list.get(i).getLoginUrl(); //登录地址
			password = MD5Util.string2MD5(password).toUpperCase(); //MD5加密
			String sessionId = HttpLogin.leShuaLogin(0,userName,password,loginUrl,imgCodeUrl);
			if(StringUtils.isBlank(sessionId)){
				//添加失败日志
				transactionInfoService.insertFailTerLog(expInfo+"-识别验证码失败",accountId);
			}else{
				transactionInfoService.addTransactionLeShua(sessionId,accountId,expInfo);
			}
		}
		logger.info("乐刷交易定时器结束=======================addTransactionLeShua");
	}

	protected void addTransactionLeShuaBig() {
		logger.info("乐刷大POS交易定时器开始=======================addTransactionLeShua");
		//查询所有渠道下面的乐刷账户信息
		List<LogAccount> list = transactionInfoService.queryTerAccountList(7);
		for (int i = 0; i < list.size(); i++) {
			//登录账号
			String userName = list.get(i).getUserName();
			//密码
			String password = list.get(i).getPassword();
			//账号id
			Long accountId = list.get(i).getAccountId();
			//说明
			String expInfo = list.get(i).getExpInfo();
			//二维码地址
			String imgCodeUrl = list.get(i).getImgCodeUrl();
			//登录地址
			String loginUrl = list.get(i).getLoginUrl();
			String token = HttpLogin.leShuaBigLogin(0,userName,password,loginUrl,imgCodeUrl);
			if(StringUtils.isBlank(token)){
				//添加失败日志
				transactionInfoService.insertFailTerLog(expInfo+"-识别验证码失败",accountId);
			}else{
				transactionInfoService.addTransactionLeShuaBig(token,accountId,expInfo);
			}
		}
		logger.info("乐刷大POS交易定时器结束=======================addTransactionLeShua");
	}

	protected void addTransactionLeRuiHeBao() {
		logger.info("瑞和宝交易定时器开始=======================addTransactionLeRuiHeBao");
		//查询所有渠道下面的乐刷账户信息
		List<LogAccount> list = transactionInfoService.queryTerAccountList(2);
		for (int i = 0; i < list.size(); i++) {
			String userName = list.get(i).getUserName(); //登录账号
			String password = list.get(i).getPassword(); //密码
			Long accountId = list.get(i).getAccountId(); //账号id
			String expInfo = list.get(i).getExpInfo();   //说明
			String imgCodeUrl = list.get(i).getImgCodeUrl();//二维码地址
			String loginUrl = list.get(i).getLoginUrl(); //登录地址
			password = MD5Util.string2MD5(password);
			String token = HttpLogin.ruiHeBaoLogin(0,userName,password,loginUrl,imgCodeUrl);
			if(StringUtils.isBlank(token)){
				//添加失败日志
				transactionInfoService.insertFailTerLog(expInfo+"-识别验证码失败",accountId);
			}else{
				transactionInfoService.addTransactionRuiHeBao(token,accountId,expInfo);
			}
		}
		logger.info("瑞和宝交易定时器结束=======================addTransactionLeRuiHeBao");
	}


	protected void addTransactionShanyin() {
		logger.info("闪银交易定时器开始=======================addTransactionShanyin");
		//查询所有渠道下面的乐刷账户信息
		List<LogAccount> list = transactionInfoService.queryTerAccountList(8);
		for (int i = 0; i < list.size(); i++) {
			//登录账号
			String userName = list.get(i).getUserName();
			//密码
			String password = list.get(i).getPassword();
			//账号id
			Long accountId = list.get(i).getAccountId();
			//说明
			String expInfo = list.get(i).getExpInfo();
			//登录地址
			String loginUrl = list.get(i).getLoginUrl();

			String token = HttpLogin.shanYinLogin(0,userName,password,loginUrl,"江苏加减信息科技有限公司");
			if(StringUtils.isBlank(token)){
				//添加失败日志
				transactionInfoService.insertFailTerLog(expInfo+"-登录失败",accountId);
			}else{
				transactionInfoService.addTransactionShanyin(token,accountId,expInfo);
			}
		}
		logger.info("闪银交易定时器结束=======================addTransactionShanyin");
	}



}
